package com.kobeliu.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.pojo.Industry;
import com.kobeliu.pojo.JobType;
import com.kobeliu.pojo.vo.JobTypeSecondAndThirdVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.service.JobTypeService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.description.method.MethodDescription;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("jobType")
public class JobTypeController extends BaseInfoProperties {

    @Autowired
    private JobTypeService jobTypeService;

    private static final String TOP_JOB_TYPE_LIST = "top_job_type_list";
    private static final String CHILD_JOB_TYPE_LIST = "child_job_type_list";


    // =======================APP START===============================
    /**
     * 获取工作类型
     * @return
     */
    @GetMapping("app/initTopList")
    public GraceJSONResult initTopList(){

        List<JobType> types = null;
        String listJson = redis.get(TOP_JOB_TYPE_LIST);
        if(StringUtils.isNotBlank(listJson)){
            types = new Gson().fromJson(listJson, new TypeToken<List<JobType>>() {}.getType());
        }else{
            types = jobTypeService.initTopLIst();
            redis.set(TOP_JOB_TYPE_LIST,new Gson().toJson(types),60);
        }

        return GraceJSONResult.ok(types);
    }

    @GetMapping("app/getSecondAndThirdListByTop/{fatherId}")
    public GraceJSONResult getSecondAndThirdListByTop(@PathVariable("fatherId")String fatherId){

        List<JobTypeSecondAndThirdVO> types = null;
        String listJson = redis.get(CHILD_JOB_TYPE_LIST+":"+fatherId);
        if(StringUtils.isNotBlank(listJson)){
            types = new Gson().fromJson(listJson, new TypeToken<List<JobTypeSecondAndThirdVO>>() {}.getType());
        }else{
            types = jobTypeService.getSecondAndThirdListByTop(fatherId);
            redis.set(CHILD_JOB_TYPE_LIST+":"+fatherId,new Gson().toJson(types),60);
        }

        return GraceJSONResult.ok(types);

    }

    // =======================APP END=================================
}



